package com.tpc.eb.utils;

import org.ini4j.Profile;
import org.ini4j.Wini;

import java.io.File;
import java.io.IOException;
import java.util.*;

public class FileUtil {
    public static String filePath = null;

    public static String getFilePath() throws IOException {
        File file =new File("");
        filePath = file.getCanonicalPath() + "/Config.ini";
        return filePath;
    }

    public static Map<String,String> getOMSConfig() throws IOException {
        filePath = FileUtil.getFilePath();
        Wini wini = new Wini(new File(filePath));
        String AppKey =  wini.get("OMSConfig", "AppKey");
        String URL =  wini.get("OMSConfig", "URL");
        String AppSecret =  wini.get("OMSConfig", "AppSecret");
        String Method = wini.get("OMSConfig", "Method");
        String SignNo = wini.get("OMSConfig", "SignNo");
        String Name = wini.get("OMSConfig","Name");
        Map<String,String> map = new HashMap<>();
        map.put("AppKey",AppKey);
        map.put("URL",URL);
        map.put("AppSecret",AppSecret);
        map.put("Method",Method);
        map.put("SignNo",SignNo);
        map.put("Name",Name);
        return map;
    }

    public static List getOMSMethod()  {
        Map<String, String> map = null;
        try {
            map = FileUtil.getOMSConfig();
            String method = map.get("Method");
            String[] split = method.split(",");
            List list = new ArrayList();
            for (String strMethod: split ) {
                list.add(strMethod);
            }
            return list;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Map<String,String> getDBConfig() throws IOException {
        filePath = FileUtil.getFilePath();
        Wini wini = new Wini(new File(filePath));
        Profile.Section section = wini.get("DBConfig");
        String dbType = section.get("dbType");
        String server = section.get("server");
        String userName = section.get("userName");
        String passWord = section.get("passWord");
        String dataBase = section.get("dataBase");
        String port = section.get("port");
        Map<String,String> map = new HashMap<>();
        map.put("dbType",dbType);
        map.put("server",server);
        map.put("userName",userName);
        map.put("passWord",passWord);
        map.put("dataBase",dataBase);
        map.put("port",port);
        return map;
    }

    public static void saveConfig(String config,Map<String,String> map) throws IOException {
        filePath = FileUtil.getFilePath();
        Wini wini = new Wini(new File(filePath));
        Iterator iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry entry = (Map.Entry)iterator.next();
            wini.add(config, (String) entry.getKey(),entry.getValue());
        }
        wini.store(new File(filePath));
    }
    //根据key删除
    public static void removeConfig(String config,String key) throws IOException {
        filePath = FileUtil.getFilePath();
        Wini wini = new Wini(new File(filePath));
        wini.remove(config,key);
        wini.store(new File(filePath));
    }

    //根据节点删除
    public static void removeConfig(String config) throws IOException {
        filePath = FileUtil.getFilePath();
        Wini wini = new Wini(new File(filePath));
        wini.remove(config);
        wini.store(new File(filePath));
    }

    public static void addConfig(String config,String key,String value) throws IOException {
        filePath = FileUtil.getFilePath();
        Wini wini = new Wini(new File(filePath));
        wini.add(config,key,value);
        wini.store(new File(filePath));
    }

    public static Profile.Section getInterfaceList(String config) throws IOException {
        filePath = FileUtil.getFilePath();
        Wini wini = new Wini(new File(filePath));
        Profile.Section section = wini.get(config);
        return section;
    }
}
